﻿using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Windows.Forms;
using ToolUtil;
using zyconfig;

namespace zymain
{
    public partial class FrmTjyd : Form
    {
        public BindingSource BookData;
        public DataTable dt = new DataTable();
        private int kjm;
        private int gjm;
        private string uid;
        private string marc = string.Empty, mainKay;
        private object mrq;
        private bool flag;
        public FrmTjyd()
        {
            InitializeComponent();
        }

        private void FrmTjyd_Load(object sender, EventArgs e)
        {
            string formName = "FrmTool";
            Form fr = System.Windows.Forms.Application.OpenForms[formName];
            if (fr != null)
            {
                FrmTool f1 = (FrmTool)fr;
                //取到Form1的值是1
                this.uid = f1.UserID;
                this.gjm = f1.Gjm;
            }
            using (DbOperHandler _open = new Common().Doh())
            {
                _open.Reset();
                _open.ConditionExpress = "期数 like N'oa%'";
                string mqs = _open.MaxString("OPAC_推荐阅读", "期数");
                mrq = _open.MaxObject("OPAC_推荐阅读", "推荐日期");
                string result = System.Text.RegularExpressions.Regex.Replace(mqs, @"[^0-9]+", "");
                string mq = mqs.Replace(result, "");
                this.tjqTxt.Text = mq + (Convert.ToInt32(result) + 1);
            }
        }
        private void FrmTjyd_Shown(object sender, EventArgs e)
        {
            this.mainSearch1.AddConfig(0,mrq);
        }
        private void mainSearch1_BtnScarchClick(object sender, EventArgs e)
        {
            if (this.wtjItem.Checked)
                this.mainSearch1.wtj = true;
            else
                this.mainSearch1.wtj = false;
            if (this.ytjItem.Checked)
                this.mainSearch1.ytj = true;
            else
                this.mainSearch1.ytj = false;
            this.Invalidate();
            this.kjm = this.mainSearch1._kjm;
            this.Cursor = Cursors.WaitCursor;
            string sql = this.mainSearch1.Sql;
            this.BookData = new BindingSource();
            using (DbOperHandler _open = new Common().Doh())
            {
                if (_open.dbType.ToString().Equals("ORACLE") || _open.odbType.ToString().Equals("ORACLE"))
                    flag = true;
                _open.Reset();
                _open.SqlCmd = sql;
                Console.WriteLine(sql);
                dt = _open.GetDataTable();
                /**
                dt.Columns.Add("tjyd");
                foreach(DataRow drow in dt.Rows)
                {
                    _open.Reset();
                    _open.ConditionExpress = "主键码=" + drow["主键码"];
                    if (_open.Exist("OPAC_推荐阅读"))
                    {
                        drow["tjyd"] = 1;
                    }
                    else
                    {
                        drow["tjyd"] = 0;
                    }
                }**/
                var dataRows = dt.AsEnumerable();
                foreach (var row in dataRows)
                {
                    var cellList = row.ItemArray.ToList();
                    row.ItemArray = cellList.Select(x => x.ToString().Trim()).ToArray();
                }
                //this.dataGridBook.Columns.Clear();
                this.dataGridBook.AutoGenerateColumns = false;
                this.BookData.DataSource = dt;
                this.binNavBook.BindingSource = this.BookData;
                this.BookData.PositionChanged += new EventHandler(this.BookData_PositionChanged);
                this.dataGridBook.DataSource = this.BookData;
                this.dataGridBook.DefaultCellStyle.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
                this.dataGridBook.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.AllCells;
                if (this.BookData.Count == 0)
                {
                    MessageBox.Show("未检索到任何记录");
                    //this.stjBtn.Enabled = false;
                }
                else
                {
                    this.dgbSetting();
                    this.BookData_PositionChanged(sender, e);
                    //this.stjBtn.Enabled = true;
                }
            }
            this.Cursor = Cursors.Default;
        }
        private void dgbSetting()
        {
            using (DbOperHandler _open = new Common().Doh())
            {
                int m = 0;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    //Console.WriteLine(dt.Rows[i]["主键码"]);
                    _open.Reset();
                    _open.ConditionExpress = "主键码=" + dt.Rows[i]["主键码"];
                    if (_open.Exist("OPAC_推荐阅读"))
                    {
                        this.dataGridBook.Rows[i].DefaultCellStyle.ForeColor = Color.White;
                        this.dataGridBook.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                        this.dataGridBook.Rows[i].Cells[0].ReadOnly = true;
                    }
                    else
                        m++;
                }
                if (m > 0)
                {
                    this.stjBtn.Enabled = true;
                    this.qxBtn.Enabled = true;
                    this.qxqxBtn.Enabled = true;
                }
                else
                {
                    this.stjBtn.Enabled = false;
                    this.qxBtn.Enabled = false;
                    this.qxqxBtn.Enabled = false;
                }
            }
        }
        private void BookData_PositionChanged(object sender, EventArgs e)
        {
            DataRowView current = this.BookData.Current as DataRowView;
            mainKay = current["主键码"].ToString().Trim();
            this.zjmTxt.Text = mainKay;
            using (DbOperHandler _open = new Common().Doh())
            {
                _open.Reset();
                _open.ConditionExpress = "主键码=" + mainKay;
                string imgurl = _open.GetField("馆藏书目库", "封面地址").ToString().Trim();
                if (!string.IsNullOrEmpty(imgurl))
                {
                    this.uimgBtn.Enabled = false;
                    imgurl = "http://lib.ccbupt.cn" + imgurl;
                    try
                    {
                        this.pictureBox1.Image = Image.FromStream(WebRequest.Create(imgurl).GetResponse().GetResponseStream());
                    }
                    catch
                    {
                        this.uimgBtn.Enabled = true;
                        string url = "http://lib.ccbupt.cn/images/book-default-medium.gif";
                        this.pictureBox1.Image = Image.FromStream(WebRequest.Create(url).GetResponse().GetResponseStream());
                    }
                }
                else
                {
                    this.uimgBtn.Enabled = true;
                    string url = "http://lib.ccbupt.cn/images/book-default-medium.gif";
                    this.pictureBox1.Image = Image.FromStream(WebRequest.Create(url).GetResponse().GetResponseStream());
                }
                _open.Reset();
                _open.ConditionExpress = "主键码=" + mainKay;
                if (_open.Exist("OPAC_推荐阅读"))
                {
                    this.qxtjBtn.Enabled = true;
                    this.label1.Text = "期数:" + _open.GetField("OPAC_推荐阅读", "期数");
                }
                else
                    this.qxtjBtn.Enabled = false;
            }
        }
        private void uimgBtn_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(mainKay))
            {
                if (MessageBox.Show("确认要选定图书上传封面图片吗？", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
                {
                    OpenFileDialog dialog = new OpenFileDialog();
                    dialog.Filter = "图片文件| *.jpg;*.png;*.jpeg";
                    //保存对话框是否记忆上次打开的目录 
                    dialog.RestoreDirectory = true;
                    if (dialog.ShowDialog() == DialogResult.OK)
                    {
                        //Console.WriteLine(dialog.FileName);
                        //FileInfo info = new FileInfo(dialog.FileName);
                        FtpTool ft = new FtpTool("ftp://lib.ccbupt.cn", "libweb", "info_200*");
                        string imgurl = ft.Upload(dialog.FileName);
                        using (DbOperHandler _open = new Common().Doh())
                        {
                            _open.Reset();
                            _open.ConditionExpress = "主键码=" + mainKay;
                            _open.AddFieldItem("封面地址", imgurl);
                            _open.Update("馆藏书目库");
                        }
                    }
                }
            }
        }
        private void wtj_CheckedChanged(object sender, EventArgs e)
        {
            if (this.wtjItem.Checked)
                this.mainSearch1.wtj = true;
            else
                this.mainSearch1.wtj = false;
        }

        private void ytj_CheckedChanged(object sender, EventArgs e)
        {
            if (this.ytjItem.Checked)
                this.mainSearch1.ytj = true;
            else
                this.mainSearch1.ytj = false;
        }

        private void dgb_CellClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void qxtjBtn_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认要取消推荐当前选定图书吗？", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                using (DbOperHandler _open = new Common().Doh())
                {
                    _open.Reset();
                    _open.ConditionExpress = "主键码=" + mainKay;
                    _open.Delete("OPAC_推荐阅读");
                }
                this.mainSearch1.AddTjqs();
            }
        }

        private void qxBtn_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridBook.Rows.Count; i++)
            {
                this.dataGridBook.Rows[i].Cells[0].Value = true;
            }
        }

        private void qxqxBtn_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridBook.Rows.Count; i++)
            {
                this.dataGridBook.Rows[i].Cells[0].Value = false;
            }
        }

        private void stjBtn_Click(object sender, EventArgs e)
        {
            string tjq = this.tjqTxt.Text.Trim();
            if (string.IsNullOrEmpty(tjq))
            {
                ShowMessageBox.ShowMessageBoxTimeout("推荐期数不能为空...", "警告", MessageBoxButtons.OK, 3000);
                this.tjqTxt.Focus();
            }
            else
            {
                if (MessageBox.Show("确认要推荐选定图书吗？", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
                {
                    using (DbOperHandler _open = new Common().Doh())
                    {
                        for (int i = 0; i < dataGridBook.Rows.Count; i++)
                        {
                            if (Convert.ToBoolean(dataGridBook.Rows[i].Cells[0].Value))
                            {
                                this.dataGridBook.Rows[i].Selected = true;
                                this.dataGridBook.CurrentCell = this.dataGridBook.Rows[i].Cells[0];
                                dgb_CellClick(dataGridBook, new DataGridViewCellEventArgs(i, 0));
                                Console.WriteLine(mainKay);
                                _open.Reset();
                                _open.ConditionExpress = "主键码=" + mainKay;
                                if (!_open.Exist("OPAC_推荐阅读"))
                                {
                                    _open.Reset();
                                    _open.AddFieldItem("主键码", mainKay);
                                    _open.AddFieldItem("推荐日期", DateTime.Now);
                                    _open.AddFieldItem("期数", tjq);
                                    _open.Insert("OPAC_推荐阅读");
                                }
                            }
                        }
                        this.mainSearch1.AddTjqs();
                    }
                }
            }
        }
    }
}
